﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
//using System.ServiceModel;
//using System.ServiceModel.Web;

namespace Asset_Service_Layer.Model
{
    [DataContract]
    public class User
    {
        [DataMember]
        private string _userName;
        [DataMember]
        private string _passWord;
        [DataMember]
        private int _pkID;

        public string Username 
        {
            get 
            {
                return _userName;
            }
            set 
            {
                Username = value;
            }
        }//end username field
        public string Password
        {
            get
            {
                return _passWord;
            }
            set
            {
                Password = value;
            }
        }//end password
        public int PKID
        {
            get
            {
                return _pkID;
            }
            //set
            //{
            //    PKID = value;
            //}
        }//end PKID

        public User(string UserName, string Password)
        {
            string sConn = Properties.Settings.Default.DBConn;
            SqlConnection conn = new SqlConnection(sConn);
            conn.Open();
            SqlCommand cmd = new SqlCommand("logInUser", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = UserName;
            cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = Password;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];            
            //validation somehow
            if (dt.Rows.Count > 0 && dt.Rows.Count < 2)
            {
                foreach (DataRow r in dt.Rows)
                {
                    _userName = r["UserName"].ToString();
                    _passWord = r["UserPassword"].ToString();
                    _pkID = Convert.ToInt32(r["pkUserID"].ToString());                
                }                
            }
            conn.Close();
            conn.Dispose();           
        }

        //have to do this elsewhere. 
        //public void enterUser(string UserName, string Password)
        //{
        //    //bool isntTaken = false;            
            
        //    string sConn = Properties.Settings.Default.DBConn;
        //    SqlConnection conn = new SqlConnection(sConn);
        //    conn.Open();
            
        //    SqlCommand cmd = new SqlCommand("UserRegister", conn);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = UserName;
        //    cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = Password;            
            
        //    //SqlDataAdapter da = new SqlDataAdapter(cmd);
        //    //DataSet ds = new DataSet();
        //    //da.Fill(ds);
        //    //DataTable dt = ds.Tables[0];

            
        //    //if (dt.Rows.Count > 0)
        //    //{
        //    //    foreach (DataRow r in dt.Rows)
        //    //    {
        //    //        _userName = r["UserName"].ToString();
        //    //        _passWord = r["UserPassword"].ToString();
        //    //        _pkID = Convert.ToInt32(r["pkUserID"].ToString());
        //    //    }
        //    //}

        //    //return user;            
            
        //    conn.Close();
        //    conn.Dispose(); 
        //}
        public User()
        {
            User user = new User();
            // TODO: Complete member initialization
        }
    }
}